package wang.aweb.childcar.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wang.aweb.childcar.exception.CarException;
import wang.aweb.childcar.exception.CarExceptionEnum;
import wang.aweb.childcar.model.dao.AdministratorMapper;
import wang.aweb.childcar.model.pojo.Administrator;
import wang.aweb.childcar.model.req.LoginReq;
import wang.aweb.childcar.service.AdministratorService;
import wang.aweb.childcar.util.Md5Utils;

import java.security.NoSuchAlgorithmException;

@Service
public class AdministratorServiceImpl implements AdministratorService {
    @Autowired
    private AdministratorMapper administratorMapper;
    @Override
    public Administrator doLogin(LoginReq loginReq) {
        String md5Password=null;
        try {
            md5Password= Md5Utils.getMD5Str(loginReq.getPassword());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        Administrator user=administratorMapper.doLogin(loginReq.getUsername(),md5Password);
        if(user==null){
            throw new CarException(CarExceptionEnum.WRONG_PARSSWORD);
        }
        return user;
    }
    @Override
    public Administrator findById(Integer id) {
        Administrator admin=administratorMapper.selectByPrimaryKey(id);
        return admin;
    }
}
